package com.ayla.demo.widgets.touch

import android.util.Log
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2

class TouchCallback(private val iTouch: ITouch? = null) : ItemTouchHelper.Callback() {

    private val tag = this.javaClass.simpleName

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        Log.i(
            tag,
            "getMovementFlags（parent = ${recyclerView.parent.parent.parent}） ==> position = ${viewHolder.bindingAdapterPosition}"
        )
        val parent = recyclerView.parent.parent.parent
        if (parent is ViewPager2) {
            parent.isUserInputEnabled = false
        }
        val dragFlags =
            ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.START or ItemTouchHelper.END or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
        val swipeFlags = 0
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    /**
     * 拖动item的回调方法，在这里面实现拖动需要做的事情，比如RecyclerView里面item的顺序交换，
     * 传入的参数是被拖动item的ViewHolder和目标ViewHolder
     * @param recyclerView
     * @param viewHolder
     * @param target
     * @return
     */
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        Log.i(
            tag,
            "onMove ==> source = ${viewHolder.bindingAdapterPosition}, target = ${target.bindingAdapterPosition}"
        )
        return iTouch?.onMove(
            viewHolder,
            viewHolder.bindingAdapterPosition,
            target,
            target.bindingAdapterPosition
        ) ?: false
    }

    /**
     * 滑动 item 的回调方法，在这里面实现滑动需要做的事情
     * @param viewHolder
     * @param direction
     */
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

    }


    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
        when (actionState) {
            ItemTouchHelper.ACTION_STATE_DRAG -> {
                Log.i(
                    tag,
                    "onSelectedChanged (DRAG) ==> position = ${viewHolder?.bindingAdapterPosition}"
                )
                iTouch?.onDrag(viewHolder!!, actionState)
            }

            ItemTouchHelper.ACTION_STATE_SWIPE -> {
                Log.i(
                    tag,
                    "onSelectedChanged  (SWIPE) ==> position = ${viewHolder?.bindingAdapterPosition}"
                )
                iTouch?.onSwipe(viewHolder!!, actionState)
            }

            ItemTouchHelper.ACTION_STATE_IDLE -> {
                Log.i(
                    tag,
                    "onSelectedChanged  (IDLE) ==> position = ${viewHolder?.bindingAdapterPosition}"
                )

            }
        }
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        Log.i(
            tag,
            "clearView（parent = ${recyclerView.parent.parent.parent}）==> position = ${viewHolder.bindingAdapterPosition}"
        )
        val parent = recyclerView.parent.parent.parent
        if (parent is ViewPager2) {
            parent.isUserInputEnabled = true
        }
        iTouch?.onEnd(recyclerView, viewHolder)
    }

    override fun isLongPressDragEnabled(): Boolean {
        return true
    }

    override fun isItemViewSwipeEnabled(): Boolean {
        return false
    }
}